<?php if (!defined('BASEPATH'))
    exit ('No direct script access allowed');
    /**
     *    会员
     * @author xiahuiqin
     *
     */
    class Member extends CI_Controller
    {
        public function  __construct()
        {
            parent::__construct();

            $this->load->model("member_mdl", 'member');

            $this->id     = $this->session->userdata('member_id');
            $this->member = $this->member->get_one_list($this->id);

        }

        public function  index()
        {
            //$this->member_list();
            //echo $this->session->userdata('member_name');
            $this->load->view('member/center');
        }

        /**
         * 用户个人中心
         */
        function center()
        {
            if (!empty($this->id)) {
                $data['member'] = $this->member;

                $this->load->view('member/personal', $data);
            } else {
                $url = base_url() . 'front/login/login';
                echo "<script>alert('您还未登录');window.location.href='" . $url . "';</script>";
                exit;
            }
        }

        /**
         * 新增会员
         */
        function member_add()
        {
            $datas          = array();
            $datas['error'] = '';

            if ($_POST) {
                $this->form_validation->set_rules('name', '用户名', 'trim|required|max_length[15]');
                $this->form_validation->set_rules('password', '密码', 'trim|required');
                $this->form_validation->set_rules('cpassword', '确认密码', 'trim|required|matches[password]');
                $this->form_validation->set_rules('email', '邮箱', 'required');

                if ($this->form_validation->run() == FALSE) {
                    $this->layout->view('member/member_addV', $datas);
                } else {
                    $datas             = $this->_post_data();
                    $datas['reg_time'] = date('Y-m-d H:i:s');
                    $datas['img']      = $this->_upload_img('img', 'local_first_add');

                    if ($this->member->insert($datas)) {
                        $datas['error'] = "成功成为会员！";
                    } else {
                        $datas['error'] = '新增失败！';
                    }
                    $this->layout->view('member/member_addV', $datas);
                }
            } else {
                $this->layout->view('member/member_addV', $datas);
            }
        }

        /**
         * 会员编辑
         */
        function member_edit()
        {
            $id = $this->uri->segment('4');

            if ($_POST) {
                $datas             = $this->_post_data();
                $datas['reg_time'] = $this->input->post('reg_time');
                $datas['img']      = $this->_upload_img('img', '1');

                if ($this->member->update_byfield('id', $id, $datas)) {
                    $data['error'] = "更新会员信息成功！";
                } else {
                    $data['error'] = '更新失败！';
                }
            } else {
                $data['error'] = '';
            }
            $data['one'] = $this->member->get_one_list($id);

            $this->layout->view('member/member_editV', $data);
        }

        /**
         * 管理员删除
         */
        function member_delete()
        {
            $id  = $this->uri->segment(4);
            $con = array('id' => $id);

            $this->member->del($con);

            $this->member_list();
        }

        function _post_data()
        {
            $datas['name']  = $this->input->post('name'); //用户名
            $datas['phone'] = $this->input->post('phone'); //联系电话
            $datas['email'] = $this->input->post('email'); //邮箱
            $datas['url']   = $this->input->post('url'); //个人链接
            $datas['title'] = $this->input->post('title'); //头衔

            $datas['gender']     = $this->input->post('gender'); //性别
            $datas['birth_date'] = $this->input->post('birth_date'); //出生日期
            $datas['address']    = $this->input->post('address'); //地址

            if (strlen($this->input->post('password')) == 32) {
                $datas['password'] = $this->input->post('password'); //密码
            } else {
                $datas['password'] = md5($this->input->post('password')); //密码
            }
            if ($this->input->post('authority')) {
                $datas['authority'] = $this->input->post('authority'); //权限
            } else {
                $datas['authority'] = 0;
            }
            if ($this->session->userdata('authority') != 1) {
                $datas['last_login_time'] = date('Y-m-d H:i:s'); //上次登入时间,即将这次的登入时间记作下次用的上次登入时间
                $datas['last_login_ip']   = $this->input->ip_address(); //上次登入ip,放在登录那一块儿做修改
            }

            return $datas;
        }

        /*
         *
        * 搜索信息
        *
        */
        function search()
        {
            if ($this->input->post('keywords') == '') {
                $where  = '';
                $limit  = '';
                $offset = '';
                $order  = 'id ASC';

                $data['result'] = $this->member->get_list($where, $limit, $offset, $order);
            } else {
                $keywords = trim($this->input->post('keywords'));
                $super    = '超级后台';
                $normal   = '普通前台';
                $stop     = '暂被罢职前后台';
                //0是前台管理员，普通管理员，1是后台管理员，是超级管理员，2是前后台管理员，是暂被罢职管理员
                if (strstr($super, $keywords)) {
                    $keywords = 1;
                } else if (strstr($normal, $keywords)) {
                    $keywords = 0;
                } else if (strstr($stop, $keywords)) {
                    $keywords = 2;
                } else {
                    $keywords = $this->input->post('keywords');
                }
                $like  = array($this->input->post('where') => $keywords);
                $order = 'id ' . $this->input->post('orderby');

                $data['result'] = $this->member->get_list('', '', '', $order, $like);
            }
            $this->layout->view('member/member_listV', $data);
        }

        function _upload_img($img_name, $img_url)
        {
            $config ['upload_path']   = './upload/avator/'; //设置头像上传目录
            $config ['allowed_types'] = 'gif|jpg|png';
            $config ['max_size']      = 1024 * 2;
            $config ['encrypt_name']  = TRUE;

            $this->load->library('upload', $config);

            if ($_FILES[$img_name]['size'] && $this->upload->do_upload($img_name)) {

                if ($img_url != 'local_first_add') { //如果不是第一次增加图片，即是更改图片
                    $url = './upload/avator/' . $img_url;
                    unlink($url); //删除原图
                }

                //取得上传成功数据
                $data = $this->upload->data();

                $img = $data['file_name'];
            } else {
                $img = $this->input->post($img_name); //图片
            }

            return $img;
        }
    }